﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace _2._2._1_处理管道
{
    /// <summary>
    /// 压根不需要那么复杂，示例中为了简单，继承了System.ComponentModel.Component
    /// 也就是为了在
    ///  public event EventHandler StartProcess
        //{
        //    add { this.ev.AddHandler(startEvent, value); }
        //    remove { this.ev.RemoveHandler(startEvent, value); }
        //}
    //Events->ev
    /// </summary>
    class Program
    {
       
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ProcessPipeline process = new ProcessPipeline();
            process.StartProcess += new EventHandler(process_PreProcess);
            process.StartProcess += new EventHandler(process_StartProcess);
            process.PreProcess += new EventHandler(process_PreProcess);
            process.PostProcess += new EventHandler(process_PostProcess);
            process.EndProcess += new EventHandler(process_EndProcess);

            process.Process();
            Console.ReadKey();
        }
        static void process_StartProcess(object sender, EventArgs e)
        {
            Console.WriteLine("开始处理的事件处理...");
        }

        static void process_PreProcess(object sender, EventArgs e)
        {
            Console.WriteLine("处理前的事件处理...");
        }

        static void process_PostProcess(object sender, EventArgs e)
        {
            Console.WriteLine("处理后的事件处理...");
        }

        static void process_EndProcess(object sender, EventArgs e)
        {
            Console.WriteLine("处理完成的事件处理...");
        }



    }
    public class ProcessPipeline
    {
        #region 作为事件键值的对象
        private static readonly object startEvent = new object();
        private static readonly object preProcessEvent = new object();
        private static readonly object postProcessEvent = new object();
        private static readonly object endEvent = new object();
        private EventHandlerList ev = new EventHandlerList();
        #endregion

        #region 事件的定义
        public event EventHandler StartProcess
        {
            add { this.ev.AddHandler(startEvent, value); }
            remove { this.ev.RemoveHandler(startEvent, value); }
        }

        public event EventHandler PreProcess
        {
            add { this.ev.AddHandler(preProcessEvent, value); }
            remove { this.ev.RemoveHandler(preProcessEvent, value); }
        }

        public event EventHandler PostProcess
        {
            add { this.ev.AddHandler(postProcessEvent, value); }
            remove { this.ev.RemoveHandler(postProcessEvent, value); }
        }

        public event EventHandler EndProcess
        {
            add { this.ev.AddHandler(endEvent, value); }
            remove { this.ev.RemoveHandler(endEvent, value); }
        }
        #endregion

        #region 触发事件的方法
        protected void OnStartProcess(EventArgs e)
        {

            if (this.ev[startEvent] != null)
            {
                (this.ev[startEvent] as EventHandler)(this, e);
            }
        }

        protected void OnPreProcess(EventArgs e)
        {
            if (this.ev[preProcessEvent] != null)
            {
                (this.ev[preProcessEvent] as EventHandler)(this, e);
            }
        }

        protected void OnPostProcess(EventArgs e)
        {
            if (this.ev[postProcessEvent] != null)
            {
                (this.ev[postProcessEvent] as EventHandler)(this, e);
            }
        }

        protected void OnEndProcess(EventArgs e)
        {
            if (this.ev[endEvent] != null)
            {
                (this.ev[endEvent] as EventHandler)(this, e);
            }
        }
        #endregion

        // 触发处理管线的方法
        public void Process()
        {
            Console.WriteLine("开始处理 ");
            this.OnStartProcess(EventArgs.Empty);
            Console.WriteLine("准备处理");
            this.OnPreProcess(EventArgs.Empty);
            Console.WriteLine("正在处理中");
            Console.WriteLine("处理完成");
            this.OnPostProcess(EventArgs.Empty);
            Console.WriteLine("结束处理");
            this.OnEndProcess(EventArgs.Empty);
        }

    }

}
